﻿using System;
using System.Linq;
using System.Transactions;
using Medora.DataContract;
using Medora.Model.Persons;
using Medora.Services.Utils;

namespace Medora.Services.Persons
{
    public class VisitsService : ServiceBase<Visit, VisitFilter>, IVisitsService
    {
        private readonly IService<DiagnosisInVisit, VisitConnectedFilter> _diagnosisService;
        private readonly IService<ServiceInVisit, VisitConnectedFilter> _servicesService;
        private readonly IService<Medication, VisitConnectedFilter> _medicationService;

        public VisitsService(
            IUow uow, 
            IUserService userService, 
            IFilterConstructor<VisitFilter, Visit> filterConstructor, 
            IService<DiagnosisInVisit, VisitConnectedFilter> diagnosisService,
            IService<ServiceInVisit, VisitConnectedFilter> servicesService,
            IService<Medication, VisitConnectedFilter> medicationService
            ) : base(uow, userService, filterConstructor)
        {
            _diagnosisService = diagnosisService;
            _servicesService = servicesService;
            _medicationService = medicationService;
            _repository = _uow.Visits;
        }

        public Visit GetVisitFilled(Guid id)
        {
            var visit = GetById(id);
            var filter = new VisitConnectedFilter {VisitId = visit.Id};
            visit.Diagnosis = _diagnosisService.Get(filter).ToList();
            visit.Medications = _medicationService.Get(filter).ToList();
            visit.Diagnosis = _diagnosisService.Get(filter).ToList();
            return visit;
        }

        public void VisitSave(Guid userId, Visit visit)
        {
           // using (var t = new TransactionScope())
           // {
                Save(userId, visit);
                /*foreach (var s in visit.Services)
                {
                    _servicesService.Save(userId, s);
                }

                foreach (var d in visit.Diagnosis)
                {
                    _diagnosisService.Save(userId, d);
                }

                foreach (var m in visit.Medications)
                {
                    _medicationService.Save(userId, m);
                }*/
             //   t.Complete();
           // }
        }

    }
}